package com.huijinhui.wlmtsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huijinhui.wlmtsys.mapper.LeaveApplicationMapper;
import com.huijinhui.wlmtsys.pojo.dtos.LeaveApplicationDTO;
import com.huijinhui.wlmtsys.pojo.entity.LeaveApplication;
import com.huijinhui.wlmtsys.pojo.vo.LeaveApplicationVO;
import com.huijinhui.wlmtsys.service.LeaveApplicationService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class LeaveApplicationServiceImpl extends ServiceImpl<LeaveApplicationMapper, LeaveApplication> implements LeaveApplicationService  {

    @Autowired
    private LeaveApplicationMapper leaveApplicationMapper;

    @Override
    public LeaveApplicationVO createLeaveApplication(LeaveApplicationDTO leaveApplicationDTO) {
        LeaveApplication leaveApplication = new LeaveApplication();
        leaveApplication.setUserId(leaveApplicationDTO.getUserId());
        leaveApplication.setLeaveType(leaveApplicationDTO.getLeaveType());
        leaveApplication.setStartDate(leaveApplicationDTO.getStartDate());
        leaveApplication.setEndDate(leaveApplicationDTO.getEndDate());
        leaveApplication.setReason(leaveApplicationDTO.getReason());
        leaveApplication.setStatus(0); // 初始状态为待审批

        LeaveApplication savedLeaveApplication = leaveApplicationMapper.insertTo(leaveApplication);
        return convertToLeaveApplicationVO(savedLeaveApplication);
    }

    @Override
    public List<LeaveApplicationVO> getUserLeaveApplications(Long userId) {
        LambdaQueryWrapper<LeaveApplication> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(LeaveApplication::getUserId, userId);
        List<LeaveApplication> leaveApplications = leaveApplicationMapper.selectList(queryWrapper);
        return leaveApplications.stream()
                .map(this::convertToLeaveApplicationVO)
                .collect(Collectors.toList());
    }

    private LeaveApplicationVO convertToLeaveApplicationVO(LeaveApplication leaveApplication) {
        LeaveApplicationVO leaveApplicationVO = new LeaveApplicationVO();
        leaveApplicationVO.setId(leaveApplication.getId());
        leaveApplicationVO.setUserId(leaveApplication.getUserId());
        leaveApplicationVO.setLeaveType(leaveApplication.getLeaveType());
        leaveApplicationVO.setStartDate(leaveApplication.getStartDate());
        leaveApplicationVO.setEndDate(leaveApplication.getEndDate());
        leaveApplicationVO.setReason(leaveApplication.getReason());
        leaveApplicationVO.setStatus(leaveApplication.getStatus());
        leaveApplicationVO.setCreateTime(leaveApplication.getCreateTime());
        leaveApplicationVO.setUpdateTime(leaveApplication.getUpdateTime());
        return leaveApplicationVO;
    }
}